/*
   Copyright (c) 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors : 
 *  Marco Riccardi
 *  2015-02-07 - initial implementation
 *
 */
package org.mizartools.example.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Calendar;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.mizartools.dli.database.Aggregate;
import org.mizartools.dli.database.AggregateNotation;
import org.mizartools.dli.database.Attribute;
import org.mizartools.dli.database.AttributeNotation;
import org.mizartools.dli.database.ConditionalRegistration;
import org.mizartools.dli.database.Definiens;
import org.mizartools.dli.database.DefinitionalTheorem;
import org.mizartools.dli.database.ExistentialRegistration;
import org.mizartools.dli.database.ForgNotation;
import org.mizartools.dli.database.IdentifyRegistration;
import org.mizartools.dli.database.Item;
import org.mizartools.dli.database.Mode;
import org.mizartools.dli.database.ModeNotation;
import org.mizartools.dli.database.PredicateNotation;
import org.mizartools.dli.database.ReductionRegistration;
import org.mizartools.dli.database.Scheme;
import org.mizartools.dli.database.Selector;
import org.mizartools.dli.database.SelectorNotation;
import org.mizartools.dli.database.Sethood;
import org.mizartools.dli.database.Structure;
import org.mizartools.dli.database.StructureNotation;
import org.mizartools.dli.database.TermAdjectiveRegistration;
import org.mizartools.dli.database.Theorem;

@SuppressWarnings("serial")
public class UtilityDliCreateDatabaseIndexesPanel extends JPanel {

	private JTextArea textArea;
	private JScrollPane scroller;
	private JProgressBar progressBar;
	private JButton button;
	private UtilityDliFrame utilityDliFrame;

	public UtilityDliCreateDatabaseIndexesPanel(UtilityDliFrame utilityDliFrame){
		super();
		this.utilityDliFrame = utilityDliFrame;
    	setLayout(new BorderLayout());
		setPreferredSize(new Dimension(500, 150));
		button = new JButton();
		AbstractAction action =  new AbstractAction() { 
	 	    public void actionPerformed(ActionEvent e) {
	 	    	textArea.setText(Calendar.getInstance().getTime().toString()+"\n\n");
	 			Job job = new Job();
	 			Thread thread = new Thread(job);
	 			thread.start();
	 	    } 
	 	}; 
	 	button.setAction(action);
	 	button.setMaximumSize(getMaximumSize());
	 	button.setText("Start");
		add(button, BorderLayout.NORTH);
		textArea = new JTextArea();
		textArea.setEditable(false);
        scroller = new JScrollPane();
		scroller.getViewport().add(textArea);
		add(scroller, BorderLayout.CENTER);
		progressBar = new JProgressBar();
		add(progressBar, BorderLayout.SOUTH);
		
	}
	
	private class Job implements Runnable {

		@Override
		public void run() {
			button.setEnabled(false);
	    	progressBar.setMaximum(22);
	    	progressBar.setValue(0);
	    	progressBar.setIndeterminate(false);
	    	String dliString = null;
	    	Boolean error = false;
			try {
				// Item
				if (!error) {printArea("Item", Item.getItemIdList().size(), 1);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Aggregate
				if (!error) {printArea("Aggregate", Aggregate.getItemNumber(), 2);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// AggregateNotation
				if (!error) {printArea("AggregateNotation", AggregateNotation.getItemNumber(), 2);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Attribute
				if (!error) {printArea("Attribute", Attribute.getItemNumber(), 3);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// AttributeNotation
				if (!error) {printArea("AttributeNotation", AttributeNotation.getItemNumber(), 4);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// ConditionalRegistration
				if (!error) {printArea("ConditionalRegistration", ConditionalRegistration.getItemNumber(), 5);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Definiens
				if (!error) {printArea("Definiens", Definiens.getItemNumber(), 6);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// DefinitionalTheorem
				if (!error) {printArea("DefinitionalTheorem", DefinitionalTheorem.getItemNumber(), 7);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// ExistentialRegistration
				if (!error) {printArea("ExistentialRegistration", ExistentialRegistration.getItemNumber(), 8);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// ForgNotation
				if (!error) {printArea("ForgNotation", ForgNotation.getItemNumber(), 9);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// IdentifyRegistration
				if (!error) {printArea("IdentifyRegistration", IdentifyRegistration.getItemNumber(), 10);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Mode
				if (!error) {printArea("Mode", Mode.getItemNumber(), 11);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// ModeNotation
				if (!error) {printArea("ModeNotation", ModeNotation.getItemNumber(), 12);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// PredicateNotation
				if (!error) {printArea("PredicateNotation", PredicateNotation.getItemNumber(), 13);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// ReductionRegistration
				if (!error) {printArea("ReductionRegistration", ReductionRegistration.getItemNumber(), 14);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Scheme
				if (!error) {printArea("Scheme", Scheme.getItemNumber(), 15);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Selector
				if (!error) {printArea("Selector", Selector.getItemNumber(), 16);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// SelectorNotation
				if (!error) {printArea("SelectorNotation", SelectorNotation.getItemNumber(), 17);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Sethood
				if (!error) {printArea("Sethood", Sethood.getItemNumber(), 18);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Structure
				if (!error) {printArea("Structure", Structure.getItemNumber(), 19);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// StructureNotation
				if (!error) {printArea("StructureNotation", StructureNotation.getItemNumber(), 20);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// TermAdjectiveRegistration
				if (!error) {printArea("TermAdjectiveRegistration", TermAdjectiveRegistration.getItemNumber(), 21);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
				// Theorem
				if (!error) {printArea("Theorem", Theorem.getItemNumber(), 22);}
		    	if (utilityDliFrame.isClosed()) {error = true;}
			} 
			catch (Exception e) {
				textArea.append("\n");
	    		textArea.append(dliString);
	    		textArea.append(" -> Error");
				textArea.append("\n");
	    		textArea.append(e.getMessage());
	    		textArea.append(e.getStackTrace().toString());
				textArea.append("\n");
	    		textArea.repaint();
	    		textArea.setCaretPosition(textArea.getText().length()-1);
			}
			textArea.append("\n");
			textArea.repaint();
			textArea.setCaretPosition(textArea.getText().length()-1);
	    	progressBar.setValue(0);
			button.setEnabled(true);
		}
		
		private void printArea(String file, int number, int progress){
			textArea.append("#"+file+" = ["+number+"]");
			textArea.append("\n");
    		textArea.repaint();
    		textArea.setCaretPosition(textArea.getText().length()-1);
	    	progressBar.setValue(progress);
		}
		
	}
	
}
